808D - Array Division - CodeForces Solution


binary search data structures implementation *1900

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define LagaKar ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define fi first
#define se second
#define precise(ans)  cout<<fixed<<setprecision(15)<<ans
#define fo(i,n) for(ll i=0;i<n;i++)
#define Fo(i,k,n) for(ll i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define sz(x) ((ll)(x).size())
#define all(x) x.begin(), x.end()
#define PI 3.1415926535897932384626
#define MOD 1000000007
#define MOD1 998244353
typedef long long ll; typedef unsigned long long ull; typedef long double lld; typedef pair<ll, ll>  pii; typedef pair<ll, ll>    pl; typedef vector<ll> vi; typedef vector<vi> vvi; typedef vector<vvi>    vvvi; typedef vector<ll>  vl; typedef vector<vl>  vvl; typedef vector<pii> vpii; typedef vector<pl>  vpl; template <typename T> using prq_mx  = priority_queue<T>; template <typename T> using prq_mn = priority_queue<T, vector<T>, greater<T>>;
const double eps = 1e-9; const ll INF = (ll) 1e9; const ll inf64 = 2e18; const ll INF64 = 9e18;
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _prll(x); cerr << endl;
#else
#define debug(x)
#endif
void _prll(ll t) {cerr << t;} void _print(ll t) {cerr << t;} void _prll(string t) {cerr << t;} void _prll(char t) {cerr << t;} void _prll(lld t) {cerr << t;} void _prll(double t) {cerr << t;} void _prll(ull t) {cerr << t;} template <class T, class V> void _prll(pair <T, V> p); template <class T> void _prll(vector <T> v); template <class T> void _prll(set <T> v); template <class T, class V> void _prll(map <T, V> v); template <class T> void _prll(multiset <T> v); template <class T, class V> void _prll(pair <T, V> p) {cerr << "{"; _prll(p.fi); cerr << ","; _prll(p.se); cerr << "}";} template <class T> void _prll(vector <T> v) {cerr << "[ "; for (T i : v) {_prll(i); cerr << " ";} cerr << "]";} template <class T> void _prll(set <T> v) {cerr << "[ "; for (T i : v) {_prll(i); cerr << " ";} cerr << "]";} template <class T> void _prll(multiset <T> v) {cerr << "[ "; for (T i : v) {_prll(i); cerr << " ";} cerr << "]";} template <class T, class V> void _prll(map <T, V> v) {cerr << "[ "; for (auto i : v) {_prll(i); cerr << " ";} cerr << "]";}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



//////////////////////////////////////////////////////////////////////////////////






 
void chal(){
  ll n;
  cin>>n;
  vl aa(n);
  ll sum=0;
  fo(i,n){
    cin>>aa[i];
    sum+=aa[i];
  }
  if(sum%2!=0){
    cout<<"NO"<<endl;
    return;
  }
  multiset<ll> bb;
  fo(i,n){
    bb.insert(aa[i]);
  }
  ll sum2=0;
  if(bb.find(sum/2)!=bb.end()){
    cout<<"YES"<<endl;
    return;
  }
  fo(i,n){
    sum2+=aa[i];
    bb.erase(bb.find(aa[i]));
    if(sum2==(sum/2)){
      cout<<"YES"<<endl;
      return;
    }else if(sum2<(sum/2)){
      ll x=(sum/2)-sum2;
      if(bb.find(x)!=bb.end()){
        cout<<"YES"<<endl;
        return;
      }
    }else{
      break;
    }
  }
  bb.clear();
  fo(i,n)bb.insert(aa[i]);
  sum2=0;
  Fo(i,n-1,-1){
    sum2+=aa[i];
    bb.erase(bb.find(aa[i]));
    if(sum2==(sum/2)){
      cout<<"YES"<<endl;
      return;
    }else if(sum2<(sum/2)){
      ll x=(sum/2)-sum2;
      if(bb.find(x)!=bb.end()){
        cout<<"YES"<<endl;
        return;
      }
    }else{
      break;
    }
  }
  cout<<"NO"<<endl;













}






//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int32_t main() {
  LagaKar;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("Error.txt", "w", stderr);
#endif
  ll  t; t = 1;
//   cin >> t;
  for (ll i = 1; i <= t; i++) {
    chal();
  } return 0;
}


Comments

Submit
0 Comments
More Questions

1547F - Array Stabilization (GCD version)
358A - Dima and Continuous Line
1385C - Make It Good
651A - Joysticks
1474D - Cleaning
1588A - Two Arrays
816A - Karen and Morning
9D - How many trees
918B - Radio Station
15A - Cottage Village
1737B - Ela's Fitness and the Luxury Number
1425H - Huge Boxes of Animal Toys
1737A - Ela Sorting Books
768C - Jon Snow and his Favourite Number
1006C - Three Parts of the Array
81A - Plug-in
276C - Little Girl and Maximum Sum
1738D - Permutation Addicts
1348B - Phoenix and Beauty
186A - Comparing Strings
1281A - Suffix Three
1421C - Palindromifier
1443A - Kids Seating
963A - Alternating Sum
1191B - Tokitsukaze and Mahjong
1612G - Max Sum Array
1459B - Move and Turn
1006F - Xor-Paths
706C - Hard problem
304C - Lucky Permutation Triple